package com.west.common.base.config;

import android.app.Application;

import com.squareup.leakcanary.LeakCanary;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.MsgConstant;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack;
import com.west.common.helper.AccountHelper;
import com.west.common.uitils.DataCleanManager;
import com.west.common.uitils.MethodsCompat;
import com.west.common.uitils.TLog;
import com.west.common.widget.SimplexToast;

import static android.os.Build.VERSION.SDK_INT;

/**
 * @author hanyulang
 * @date 2018/10/19
 */
public class BaseApplication extends Application {

    protected static BaseApplication mInstance;
    protected PushAgent mPushAgent;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        CrashReport.initCrashReport(getApplicationContext(), "3204a22ccf", true);
        AccountHelper.init(this);
        SimplexToast.init(this);
        initUMeng();
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        LeakCanary.install(this);

    }

    protected void initUMeng(){
        UMConfigure.init(this, "5c3c4be1f1f55666d9000188", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "0eb4efadb28ea7e7dbf60a900a2b119c");

        mPushAgent = PushAgent.getInstance(this);
        mPushAgent.register(new IUmengRegisterCallback() {
            @Override
            public void onSuccess(String deviceToken) {
                TLog.error("deviceToken " + deviceToken);
                AccountHelper.setRegId(deviceToken);
            }

            @Override
            public void onFailure(String s, String s1) {
                TLog.error("onFailure " + s + " ---  " + s1);
            }
        });

        /** 服务端控制声音 **/
        mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SERVER);
        /** 客户端允许呼吸灯点亮 **/
        mPushAgent.setNotificationPlayLights(MsgConstant.NOTIFICATION_PLAY_SDK_ENABLE);
        /** 客户端禁止振动 **/
        mPushAgent.setNotificationPlayVibrate(MsgConstant.NOTIFICATION_PLAY_SDK_DISABLE);
        setAlias();
    }

    public void setAlias(){
        try {
            if(AccountHelper.isLogin()){
                mPushAgent.setAlias(AccountHelper.getUserId(), "自定义类型", new UTrack.ICallBack() {
                    @Override
                    public void onMessage(boolean isSuccess, String message) {
                        TLog.error("setAlias " + isSuccess + " " + message);
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static BaseApplication getInstance(){
        return mInstance;
    }

    /**
     * 清除app缓存
     */
    public void clearAppCache() {
        // 清除数据缓存
        DataCleanManager.cleanInternalCache(this);
        // 2.2版本才有将应用缓存转移到sd卡的功能
        if (isMethodsCompat(android.os.Build.VERSION_CODES.FROYO)) {
            DataCleanManager.cleanCustomCache(MethodsCompat
                    .getExternalCacheDir(this));
        }

    }

    /**
     * 判断当前版本是否兼容目标版本的方法
     *
     * @param VersionCode
     * @return
     */
    public static boolean isMethodsCompat(int VersionCode) {
        int currentVersion = SDK_INT;
        return currentVersion >= VersionCode;
    }



}
